package com.cysnake.quitsmoke.common.dao;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cysnake.quitsmoke.VO.CostVO;
import com.cysnake.quitsmoke.tools.FormatTools;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CostDao extends BaseDao{
	
	public static final String TABLE_NAME = "cost";
	public static final String KEY_ID = "id";
	public static final String KEY_NAME = "name";
	public static final String KEY_COSTNUM = "costnum";
	public static final String KEY_BUYTIME = "buytime";

	private static final String TAG = "CostDao";
	
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private final Context mCtx;

	public CostDao(Context ctx) {
		this.mCtx = ctx;
	}

	public CostDao open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long create(CostVO cost) {
		ContentValues initialValues = new ContentValues();
		if(cost.getName()!=null){
			initialValues.put(KEY_NAME, cost.getName());
		}
		if(cost.getCostnum()!=null){
			initialValues.put(KEY_COSTNUM, cost.getCostnum());
		}else{
			return -1;
		}
		if(cost.getBuytime()!=null){
			initialValues.put(KEY_BUYTIME,
					FormatTools.dateToString(cost.getBuytime()));
		}
		return mDb.insert(TABLE_NAME, null, initialValues);
	}

	public boolean deleteCostById(long rowId) {

		return mDb.delete(TABLE_NAME, KEY_ID + " = " + rowId, null) > 0;
	}

	public ArrayList<CostVO> getAllCost() {
		Cursor cursor = mDb.query(TABLE_NAME, new String[] { KEY_ID, KEY_NAME,
				KEY_COSTNUM, KEY_BUYTIME }, null, null, null, null, null);
		return changeCursor(cursor);

	}

	public CostVO getCostById(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, TABLE_NAME, new String[] { KEY_ID,
				KEY_NAME, KEY_COSTNUM, KEY_BUYTIME }, KEY_ID + " = " + rowId,
				null, null, null, null, null);
		List<CostVO> list = changeCursor(mCursor);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}

	}

	public boolean update(CostVO cost) {
		ContentValues args = new ContentValues();
		if(cost.getName()!=null){
			args.put(KEY_NAME, cost.getName());
		}
		if(cost.getCostnum()!=null){
			args.put(KEY_COSTNUM, cost.getCostnum());
		}
		if(cost.getBuytime()!=null){
			args.put(KEY_BUYTIME, FormatTools.dateToString(cost.getBuytime()));
		}
		return mDb.update(TABLE_NAME, args, KEY_ID + "=" + cost.getId(), null) > 0;
	}

	public int deleteAll() {
		int i = mDb.delete(TABLE_NAME, null, null);
		return i;
	}

	private ArrayList<CostVO> changeCursor(Cursor cursor) {
		ArrayList<CostVO> list = new ArrayList<CostVO>();
		while (cursor.moveToNext()) {
			CostVO cost = new CostVO();
			int temp = cursor.getColumnIndex(KEY_NAME);
			cost.setName(cursor.getString(temp));
			temp = cursor.getColumnIndex(KEY_ID);
			cost.setId(cursor.getInt(temp));
			temp = cursor.getColumnIndex(KEY_BUYTIME);
			try {
				if(cursor.getString(temp) != null){
					cost.setBuytime(FormatTools.stringToDate(cursor.getString(temp)));
				}else{
					cost.setBuytime(null);
				}
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			temp = cursor.getColumnIndex(KEY_COSTNUM);
			cost.setCostnum(cursor.getDouble(temp));
			list.add(cost);
		}
		return list;
	}
}
